/**
 * Created by taohj on 2014/5/21.
 */

var model = new Object();

/**
 * 模块列表
 * */
model.initgrid = function () {

   model.treegrid = $("#sys_model_list_datagrid");

    model.treegrid.treegrid({
        fit: true,
        url: functions.get("SysModelList")+"",
        idField: 'id',
        treeField: 'text',
        singleSelect: true,
        collapsible: true,
        pagination: false,
        rownumbers: true,
        fitColumns: true,
        nowrap:false,
        fit: true,
        animate: true,
        onRowContextMenu:function(e,rowIndex,rowData){
            e.preventDefault();
            $(this).treegrid('select', row.id)
            alert(rowData.name);
        },
        onDblClickRow:function(row){
            model.treegrid.treegrid("toggle",row.id);
        },
        toolbar: [
            {
                iconCls: 'icon-add',
                text: 'Create',
                handler: function () {
                    var selected=model.treegrid.treegrid("getSelected");
                    var parentId="";
                    if(selected){
                        parentId=selected.id;
                    }


                    console.info(parentId);
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title:'SysModelCreate',
                        href: functions.get("SysModelCreateView")+'?parentId='+parentId,
                        modal: true,
                        width: 320
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: 'Modify',
                handler: function () {
                    var selectRow= model.treegrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }
                    var id="";
                    if(selectRow){
                        id=selectRow.id;
                    }

                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: 'SysModelModify',
                        href:  functions.get("SysModelModifyView")+'?id='+id,
                        modal: true,
                        width: 320
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: 'Remove',
                handler: function () {

                   var selectRow= model.treegrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                    $.messager.confirm('Confirm', 'Are you sure remove selected object?', function(r){
                        if (r){
                            $.post(functions.get("SysModelRemove"), {
                                id:selectRow.id
                            }, function (result) {
                                model.requestResultHandlerAdapter(result,null,model.treegrid);
                            });
                        }
                    });
                    console.info(selectRow);
                }
            }
        ],
        columns: [
            [
                {field: "id", title: "ID", width: 40, hidden: true},
                {field: "text", title: "TEXT", width: 300},
                {field: "url", title: "URL", width: 280},
                {field: "type", title: "Type", width: 100,formatter:function(value){
                    return util.decode(value,[[1,'FunctionMenu'],[2,'Function']],'Unkown');
                }},
                {field: "isLeaf", title: "ISLEAF", width: 50, formatter: function (value) {
                    return value == "1";
                }},
                {field: "parent", title: "PARENT", width: 200, formatter: function (value, data, index) {
                    return data.parent ? data.parent.text : null;
                }, sortable: true},

                {field: "sort", title: "SORT", width: 50},
                {field: "description", title: "DESCRIPTION", width: 150}

            ]
        ]
    });
};





//新建提交
model.createSubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get("SysModelCreate"), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            model.requestResultHandlerAdapter(result,dialog,model.treegrid);
        });
    }
};


//修改提交
model.modifySubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get("SysModelModify"), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            model.requestResultHandlerAdapter(result,dialog,model.treegrid)
        });
    }
};

//
model.requestResultHandlerAdapter=function(result,dialog,grid){
    result=util.requestUtil(result,dialog,grid);
    if(result.success){
        functionMenu.tree("reload");
    }

    initFuntions();

}






